package com.xxg.renrenorder.strategy;

import com.xxg.renrencommon.domain.filedenum.orderenum.OrderStatusEnum;
import com.xxg.renrencommon.exception.OrderManagerException;
import com.xxg.renrencommon.exception.exceptionenum.OrderExceptionCodeCodeEnum;
import com.xxg.renrenorder.dao.bean.AfterSalesOrderInfo;
import com.xxg.renrenorder.dao.bean.OrderMasterInfo;

import java.util.Date;

/**
 * 退货退款
 *
 * @author szj
 * @date 2021/12/17
 */
public class ReturnMoneyAndProductStrategy extends AbstractAfterSalesStrategy {
    @Override
    public void handlerAfterSalesOrderInfo(AfterSalesOrderInfo afterSalesOrderInfo, OrderMasterInfo orderMasterInfo, boolean isApply) throws OrderManagerException {
        String description = "买家申请售后；订单状态：" + getOrderStatusName(orderMasterInfo.getOrderStatus()) + "；售后类型：退货退款；" + getReason(afterSalesOrderInfo);
        if (!isApply) {
            description = "买家修改申请，售后类型：退货退款 " + getReason(afterSalesOrderInfo);
        }
        setReturnProduct(true);
        // 支持订单状态（待收货、已完成15天内）
        if (orderMasterInfo.getOrderStatus() == OrderStatusEnum.UN_COMMIT.getValue()
                || orderMasterInfo.getOrderStatus() == OrderStatusEnum.SUCCESS.getValue()) {
            // 检查售后原因是否匹配
            checkReason(orderMasterInfo.getOrderStatus(), afterSalesOrderInfo.getAfterSalesType(), afterSalesOrderInfo.getReasonType(), 15);
            // 已完成15天内判断
            Date now = new Date();
            checkExpired(now, orderMasterInfo, FIFTEEN_DAYS);
            // 查询当前售后类型是否被永久拒绝
            hasForeverRejected(orderMasterInfo, afterSalesOrderInfo);
            // 查询是否存在正在处理的售后申请
            hasUnFinishAfterSalesOrder(orderMasterInfo, afterSalesOrderInfo);
            // 查询是否已处理过仅退款(协商退款)的售后
            hasDoneBefore(orderMasterInfo, afterSalesOrderInfo);
            // 查询是否处理过其他的售后申请
            hasDoneOtherAfterSales(orderMasterInfo, afterSalesOrderInfo);
            // 查询此类型支持的售后最大件数
            int maxNumber = checkApplyNumber(orderMasterInfo, afterSalesOrderInfo);
            // 设置退款金额
            checkAndSetReturnPay(afterSalesOrderInfo, maxNumber);
            // 插入进度
            setStep(now, afterSalesOrderInfo, description, 0);
        } else {
            throw new OrderManagerException(OrderExceptionCodeCodeEnum.ORDER_STATUS_ERROR2);
        }
    }
}
